﻿using System;
using System.Linq;
using System.Collections;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows;
using EnsureWeb.Web.DataModel;

namespace EnsureWeb.UI.Controls
{
    public partial class AdminRoleList : UserControl
    {

        public IEnumerable<Role> ItemsSource
        {
            get { return (IEnumerable<Role>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable<Role>), typeof(AdminRoleList), new PropertyMetadata(null, ItemsSourceChanged));

        private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
                return;
            
            var self = d as AdminRoleList;

            self.internalModels = ((IEnumerable<Role>)e.NewValue).Select(r => new InternalModel() { Value = r }).ToList();

            self.roleList.ItemsSource = self.internalModels;
        }

        public string CheckedIds
        {
            get { return (string)GetValue(CheckedIdsProperty); }
            set { SetValue(CheckedIdsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CheckedIds.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckedIdsProperty =
            DependencyProperty.Register("CheckedIds", typeof(string), typeof(AdminRoleList), new PropertyMetadata(string.Empty, CheckedIdsChanged));

        private static void CheckedIdsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var self = d as AdminRoleList;
            var roleString = e.NewValue as string;
            if (string.IsNullOrEmpty(roleString))
                return;

            var roles = roleString.Split(',');
            foreach (var internalModel in self.internalModels)
            {
                if (roles.Contains(internalModel.Value.Id.ToString()))
                    internalModel.Selected = true;
            }
        }
 
        
        public AdminRoleList()
        {
            InitializeComponent();
        }

        private IEnumerable<InternalModel> internalModels;

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            this.UpdateCheckedIds();
        }

        private void checkAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var internalModel in this.internalModels)   
                    internalModel.Selected = (bool)this.checkAll.IsChecked;

            this.UpdateCheckedIds();
        }

        private void UpdateCheckedIds()
        {
            var checkedIds = string.Join(",", this.internalModels.Where(i => i.Selected).Select(i => i.Value.Id).ToArray());

            this.SetValue(CheckedIdsProperty, checkedIds);
        }
    }

    public class InternalModel : GalaSoft.MvvmLight.ViewModelBase
    {
        public Role Value { get; set; }

        private bool _selected = false;
        public bool Selected
        {
            get { return _selected; }
            set
            {
                _selected = value;
                RaisePropertyChanged("Selected");
            }
        }
    }
}
